<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Orderinfo;
use Illuminate\Support\Facades\Auth;
use App\Repositories\OrderinfoRepository;
use App\Http\Requests\Admin\Orderinfo\ShowRequest;
use App\Http\Requests\Admin\Orderinfo\QueryResource;
use App\Http\Requests\Admin\Orderinfo\OrderinfoCreate;
use App\Http\Requests\Admin\Orderinfo\OrderinfoUpdate;
use App\Http\Requests\Admin\Orderinfo\OrderinfoDestroy;

class OrderinfoController extends Controller
{
    protected $orderinfoRepository;

    /**
     * OrderinfosController constructor.
     * @param OrderinfoRepository $orderinfoRepository
     */
    public function __construct(
        OrderinfoRepository $orderinfoRepository
    )
    {
        $this->orderinfoRepository = $orderinfoRepository;
    }

    /**
     * 获取订单信息列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $service_id = $request->get('service_id', false);
        $limit = $request->get('area_id', false);
        $orderinfos = $this->orderinfoRepository->getOrderinfos($service_id, $limit);
        return success($orderinfos);
    }

    /**
     * 获取订单信息详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, Orderinfo $orderinfo)
    {
        return success($orderinfo);
    }


    /**
     * 创建订单信息
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param OrderinfoCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(OrderinfoCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $orderinfos = $this->orderinfoRepository->create($attributes);
        return stored($orderinfos);
    }

    /**
     * 更新订单信息资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param OrderinfoUpdate $request
     * @param orderinfo $orderinfo
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(OrderinfoUpdate $request, Orderinfo $orderinfo)
    {
        $orderinfosAttr = $request->only([
            'order_id',
            'category_id',
            'price',
            'weight',
            'volume',
            'calculation',
            'courier_money',
            'service_money',
            'express_money',
            'pack_money',
            'platform_money',
            'actual_weight',
            'region_mark',
        ]);
        $orderinfo->update($orderinfosAttr);
        return updated($orderinfo);
    }

    /**
     * 删除 订单信息 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param OrderinfoDestroy $request
     * @param Orderinfo $orderinfo
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(OrderinfoDestroy $request, Orderinfo $orderinfo)
    {
        $force = $request->get('force');
        $force ? $orderinfo->delete() : $orderinfo->forceDelete();
        return deleted();
    }
}
